﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using RecipeProject.Model;
using System.Data;

namespace RecipeProject.DataAccess
{
    /// <summary>
    /// Represents a source of Recipes in the application.
    /// </summary>
    public class RecipeDatabase
    {
        #region Fields

        readonly List<Recipe> _recipes;

        #endregion // Fields

        #region Constructor

        /// <summary>
        /// Creates a new repository of Recipes.
        /// </summary>
        public RecipeDatabase(/*DataSet RecipeDataSet*/)
        {
            _recipes = new List<Recipe>
            {
                new Recipe{
                    Name="Fall Vegetable Soup with Black-Eyed Peas and Grilled Chicken",
                    Instructions="1.Lightly oil the grill rack and heat the grill to medium. Grill the chicken for 15 minutes or until the chicken is cooked through, turning the chicken halfway through cooking. Remove the chicken to a cutting surface. Let cool for 5 minutes. Cut into strips. Set aside." + System.Environment.NewLine + 
                                    "2.Heat the oil in a 6-quart saucepot over medium heat. Add the onions, carrots, parsnips and fennel. Cook for 5 minutes or until the vegetables are tender. Add the broth and heat to a boil. Reduce the heat to medium-low and cook for 30 minutes or until the vegetables are very tender." + System.Environment.NewLine +
                                    "3.Stir in the chicken, peas and parsley and heat through. Season to taste. Divide the soup mixture among 8 serving bowls.",
                    Ingredients=new List<Ingredient>{
                        new Ingredient{
                            Name="skinless, boneless chicken breast",
                            Quantity=3m/4m,
                            Measurement="pound"},
                        new Ingredient{
                            Name="olive oil",
                            Quantity=2,
                            Measurement="tablespoons"},
                        new Ingredient{
                            Name="large onions, diced",
                            Quantity=2},
                        new Ingredient{
                            Name="large carrots, diced",
                            Quantity=3}}},
                new Recipe{
                    Name="Barbie's Tuna Salad ",
                    Instructions="1.In a medium bowl, stir together the tuna, mayonnaise, Parmesan cheese, and onion flakes. Season with curry powder, parsley, dill and garlic powder. Mix well and serve with crackers or on a sandwich.",
                    Ingredients=new List<Ingredient>{
                        new Ingredient{
                            Name="(7 ounce) can white tuna, drained and flaked",
                            Quantity=2},
                        new Ingredient{
                            Name="mayonnaise or salad dressing",
                            Quantity=6,
                            Measurement="tablespoons"},
                        new Ingredient{
                            Name="Parmesan cheese",
                            Quantity=1,
                            Measurement="tablespoon"},
                        new Ingredient{
                            Name="sweet pickle relish",
                            Quantity=3,
                            Measurement="tablespoons"}}}
            };


        }

        #endregion // Constructor

        #region Public Interface

        /// <summary>
        /// Raised when a recipe is placed into the repository.
        /// </summary>
        public event EventHandler<RecipeAddedEventArgs> RecipeAdded;

        /// <summary>
        /// Places the specified r into the repository.
        /// If the r is already in the repository, an
        /// exception is not thrown.
        /// </summary>
        public void AddRecipe(Recipe recipe)
        {
            if (recipe == null)
                throw new ArgumentNullException("recipe");

            if (!_recipes.Contains(recipe))
            {
                _recipes.Add(recipe);

                if (this.RecipeAdded != null)
                    this.RecipeAdded(this, new RecipeAddedEventArgs(recipe));
            }
        }

        /// <summary>
        /// Returns true if the specified recipe exists in the
        /// repository, or false if it is not.
        /// </summary>
        public bool ContainsRecipe(Recipe recipe)
        {
            if (recipe == null)
                throw new ArgumentNullException("recipe");

            return _recipes.Contains(recipe);
        }

        /// <summary>
        /// Returns a shallow-copied list of all Recipes in the repository.
        /// </summary>
        public List<Recipe> GetRecipes()
        {
            return new List<Recipe>(_recipes);
        }

        #endregion // Public Interface

        //#region Private Helpers

        //static List<Recipe> LoadRecipes(DataSet RecipeDataSet)
        //{
        //    var recipes = RecipeDataSet.Tables["Recipe"].AsEnumerable();
        //    var ingredients = RecipeDataSet.Tables["Ingredient"].AsEnumerable();
        //    var recipeIngredient = RecipeDataSet.Tables["RecipeIngredient"].AsEnumerable();

        //    var recipeList = new List<Recipe>();

        //    foreach (var row in recipes) {
        //        var recipeID = row.Field<String>("RecipeID");
        //        var ingredientIDs =
        //            from i in recipeIngredient
        //            where i.Field<String>("RecipeID") == recipeID
        //            select i.Field<String>("IngredientID");
        //        var ingredientList =
        //            from i in ingredients
        //            join i2 in ingredientIDs on i.Field<String>("IngredientID") equals i2
        //            select new Ingredient{
        //                Name=i.Field<String>("IngredientName"),
        //                Quantity=new Mathematics.RationalNumbers.Rational(
        //                    i.Field<int>("QtyNum"),
        //                    i.Field<int>("QtyDenom")),
        //                Measurement=i.Field<String>("IngredientUOM")};
                
        //        recipeList.Add(
        //            new Recipe{
        //                Name=row.Field<String>("RecipeName"),
        //                Ingredients=ingredientList.ToList(),
        //                Instructions=row.Field<String>("RecipeInstructions")});
                
        //    }//end foreach

        //    return recipeList;

        //}//end LoadRecipes

        //#endregion // Private Helpers
    }
}